﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.tiyma.components;
using System.Collections;

namespace com.tiyma.ui
{
    public partial class FrmServices : Form
    {
        string m_service_file_name = "client-3.exe";
        public FrmServices()
        {
            InitializeComponent();
        }

        private void btnSetUp_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceManager t_service_mgr = new ServiceManager();
                Hashtable t_service_hash = new Hashtable();
                bool t_result = t_service_mgr.InstallService(t_service_hash, m_service_file_name);
                if (t_result)
                {
                    txtInfo.Text = "服务安装成功,已经在运行";
                }
                else
                {
                    txtInfo.Text = "服务安装失败,已记录异常信息";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("安装服务失败" + ex.ToString());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceManager t_service_mgr = new ServiceManager();

                bool t_result = t_service_mgr.UnInstallService(m_service_file_name);
                if (t_result)
                {
                    txtInfo.Text = "服务已卸载";
                }
                else
                {
                    txtInfo.Text = "卸载服务失败,已记录异常信息";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("安卸载服务失败" + ex.ToString());
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceManager t_service_mgr = new ServiceManager();

                bool t_result = t_service_mgr.StartService();
                if (t_result)
                {
                    txtInfo.Text = "服务已启动";
                }
                else
                {
                    txtInfo.Text = "服务启动失败,请确定服务是否已安装";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("启动服务失败" + ex.ToString());
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceManager t_service_mgr = new ServiceManager();

                bool t_result = t_service_mgr.StopService();
                if (t_result)
                {
                    txtInfo.Text = "服务已停止";
                }
                else
                {
                    txtInfo.Text = "服务停止失败,请确定服务是否已安装";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("停止服务失败" + ex.ToString());
            }
        }

        private void btnStatus_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceManager t_service_mgr = new ServiceManager();
                System.ServiceProcess.ServiceController t_status = t_service_mgr.GetServiceStatus();
                if (t_status == null)
                {
                    txtInfo.Text = "服务不存在";
                }
                else if (t_status.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    txtInfo.Text = "服务正在运行";
                }
                else if (t_status.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    txtInfo.Text = "服务已停止";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询服务状态失败" + ex.ToString());
            }
        }
    }
}
